class StyledTextCtrl < Wx::StyledTextCtrl
  attr_accessor :eol_visible,:ws_visible
  def initialize(*args)
	  
    super(*args)
    font = Wx::Font.new(10, Wx::TELETYPE, Wx::NORMAL, Wx::NORMAL)
    self.style_set_font(Wx::STC_STYLE_DEFAULT, font);

    @ws_visible = false
    @eol_visible = false
    self.set_edge_mode(Wx::STC_EDGE_LINE)

    line_num_margin = self.text_width(Wx::STC_STYLE_LINENUMBER, "_99999")
    self.set_margin_width(0, line_num_margin)

    self.style_set_foreground(Wx::STC_STYLE_DEFAULT, Wx::BLACK);
    self.style_set_background(Wx::STC_STYLE_DEFAULT, Wx::WHITE);
    self.style_set_foreground(Wx::STC_STYLE_LINENUMBER, Wx::LIGHT_GREY);
    self.style_set_background(Wx::STC_STYLE_LINENUMBER, Wx::WHITE);
    self.style_set_foreground(Wx::STC_STYLE_INDENTGUIDE,Wx::LIGHT_GREY);

    self.set_tab_width(4)
    self.set_use_tabs(false)
    self.set_tab_indents(true)
    self.set_back_space_un_indents(true)
    self.set_indent(4)
    self.set_edge_column(80)

    self.set_lexer(Wx::STC_LEX_RUBY)
    self.style_clear_all
    self.style_set_foreground(2, Wx::RED)
    self.style_set_foreground(3, Wx::GREEN)
    self.style_set_foreground(5, Wx::BLUE)
    self.style_set_foreground(6, Wx::RED)
    self.style_set_foreground(7, Wx::BLUE)
    self.set_key_words(0, "begin break elsif module retry unless end case next return until class ensure nil self when def false not super while alias defined? for or then yield and do if redo true else in rescue undef")

    self.set_property("fold","1")
    self.set_property("fold.compact", "0")
    self.set_property("fold.comment", "1")
    self.set_property("fold.preprocessor", "1")

    self.set_margin_width(1, 0)
    self.set_margin_type(1, Wx::STC_MARGIN_SYMBOL)
    self.set_margin_mask(1, Wx::STC_MASK_FOLDERS)
    self.set_margin_width(1, 20)

    self.marker_define(Wx::STC_MARKNUM_FOLDER, Wx::STC_MARK_PLUS)
    self.marker_define(Wx::STC_MARKNUM_FOLDEROPEN, Wx::STC_MARK_MINUS)
    self.marker_define(Wx::STC_MARKNUM_FOLDEREND, Wx::STC_MARK_EMPTY)
    self.marker_define(Wx::STC_MARKNUM_FOLDERMIDTAIL, Wx::STC_MARK_EMPTY)
    self.marker_define(Wx::STC_MARKNUM_FOLDEROPENMID, Wx::STC_MARK_EMPTY)
    self.marker_define(Wx::STC_MARKNUM_FOLDERSUB, Wx::STC_MARK_EMPTY)
    self.marker_define(Wx::STC_MARKNUM_FOLDERTAIL, Wx::STC_MARK_EMPTY)
    self.set_fold_flags(16)

    self.set_margin_sensitive(1,1)

    evt_stc_charadded(self.get_id) {|evt| onCharadded(evt)}
    evt_stc_marginclick(self.get_id) {|evt| onMarginClick(evt)}
  end
    
  def onWhitespace
    @ws_visible = !@ws_visible
    self.set_view_white_space(@ws_visible ? Wx::STC_WS_VISIBLEALWAYS : Wx::STC_WS_INVISIBLE)
  end

  def onEOL
    @eol_visible = !@eol_visible
    self.set_view_eol(@eol_visible)
  end

  def onCharadded(evt)
    chr =  evt.get_key
    curr_line = self.get_current_line

    if(chr == 13)
        if curr_line > 0
          line_ind = self.get_line_indentation(curr_line - 1)
          if line_ind > 0
            self.set_line_indentation(curr_line, line_ind)
            self.goto_pos(self.position_from_line(curr_line)+line_ind)
          end
        end
    end
  end

  def onMarginClick(evt)
    line_num = self.line_from_position(evt.get_position)
    margin = evt.get_margin

    if(margin == 1)
      self.toggle_fold(line_num)
    end
  end
    
end
  
    
